package com.fuang.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.stream.Collectors;

/**
 * Created by Fuang on 2017-6-27.
 *
 * Intermediate：
 * map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
 * Terminal：
 * forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
 * Short-circuiting：
 * anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit
 *
 *
 */
public class StreamDemo {
    public static void main(String[] args) {
        StreamDemo streamDemo = new StreamDemo();
        streamDemo.map();
        List<People> fruits = new ArrayList<People>();
        List<? super People> people = fruits;
        Base base = (Base) people.get(0);
        People s = (People) people.get(0);

        List<People> peopleList = new ArrayList<>();
        List<? extends Base> bases = peopleList;
        Base base1 = bases.get(0);
        //bases.add(new People());

        List<Base> test = new ArrayList<>();

        Stack<Number> numberStack = new Stack<Number>();
    }

    /**
     * filter方法相当于for循环里增加了if判断过滤
     * @param list
     * @return
     */
    public List filter(List<People> list) {
        // 过滤不是男人的对象，返回是男人的对象List:filter入参为返回boolean的函数
        List<People> peopleList = list.stream().filter(people -> people.isMan()).collect(Collectors.toList());

        // 传统写法
        List<People> peopleList1 = new ArrayList<>();
        for (People people : list) {
            if (people.isMan()) {
                peopleList1.add(people);
            }
        }

        return peopleList;
    }

    public List map() {
        List<String> list1 = Arrays.asList("abc", "bcd");
        List<String> output = list1.stream().
                map(String::toUpperCase).
                collect(Collectors.toList());
        output.stream().forEach(System.out :: println);

        //
        List<String> output1 = new ArrayList<>();
        for (String str : list1) {
            output1.add(str.toUpperCase());
        }

        return output;
    }
}
